Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: integrate vitest matchers globally #3425

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

starc007
Copy link

Summary

This PR integrates Vitest matchers globally for fuel-gauge tests, improving test maintainability and type safety.

Changes

  • Add global setup for vitest matchers in test/setup.ts
  • Add proper TypeScript types for custom matcher toEqualBn
  • Remove individual expect.extend() calls from test files
  • Remove @ts-expect-error comments for toEqualBn usage

Testing

  • ✅ All existing tests pass
  • ✅ Type checking passes without errors
  • ✅ Matcher functionality remains unchanged

Copy link

vercel bot commented Nov 26, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
ts-docs-api ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 29, 2024 8:59pm

@CLAassistant
Copy link

CLAassistant commented Nov 26, 2024

CLA assistant check
All committers have signed the CLA.

Copy link

vercel bot commented Nov 26, 2024

@starc007 is attempting to deploy a commit to the Fuel Labs Team on Vercel.

A member of the Team first needs to authorize it.

Copy link

codspeed-hq bot commented Nov 27, 2024

CodSpeed Performance Report

Merging #3425 will improve performances by 47.93%

Comparing starc007:starc007/feat/integrate-vitest-matchers-globally (b142a13) with master (d29e884)

Summary

⚡ 1 improvements
✅ 17 untouched benchmarks

Benchmarks breakdown

Benchmark master starc007:starc007/feat/integrate-vitest-matchers-globally Change
Instantiate from an address 576.7 µs 389.8 µs +47.93%

Copy link
Contributor

@Torres-ssf Torres-ssf left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @starc007,

Thank you so much for this contribution. We really appreciate it 🙏

It appears that the current solution hasn’t resolved the TS errors.

Screenshot 2024-11-27 at 9 13 42 AM

@Torres-ssf Torres-ssf added chore Issue is a chore good first issue Suitable for newcomers looking to contribute and removed feat Issue is a feature labels Nov 27, 2024
@Torres-ssf Torres-ssf changed the title feat: integrate vitest matchers globally chore: integrate vitest matchers globally Nov 27, 2024
Copy link
Contributor

@danielbate danielbate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR also needs a changeset, please run pnpm changeset from the root directory.

@starc007
Copy link
Author

Hey @Torres-ssf

Issue:
When running pnpm lint (type:check-tests), the type checker fails because the matchers aren't being recognized globally. However, when importing setupTestMatchers directly in the test file (e.g. abi-coder.test.ts), it works as expected.

This suggests the setupFiles configuration in vitest.shared.config.mts isn't being properly loaded during type checking.

Questions:

  1. Is there a different way we should be configuring the global setup file for both runtime and type checking?
  2. Should we be using a different approach to register global matchers that works with the type system?

Any guidance on the correct way to set this up would be appreciated.

},
"include": ["src", "test"]
"include": ["src", "global.d.ts"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"include": ["src", "global.d.ts"]
"include": ["src", "test", "global.d.ts"]

We want to include the test directory still.

Comment on lines +4 to +14
interface Matchers<R = BN> {
toEqualBn: (expected: BNInput) => R;
}

const pass = received.eq(argument);

if (pass) {
return {
message: () => `Expected ${received.toString()} not to equal ${argument.toString()}`,
pass: true,
};
declare module 'vitest' {
interface Assertion extends Matchers {}
interface AsymmetricMatchersContaining extends Matchers {}
interface ExpectStatic {
toEqualBn(expected: BNInput): BN;
}
return {
message: () => `expected ${received.toString()} to equal ${argument.toString()}`,
pass: false,
};
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Replace the contents of packages/fuel-gauge/test/global.d.ts with this.
  • Add the following to the tsconfig.test.json
    git cherry-pick 8ed2d14d5f57581746b260b4d83364af3a01c479.

I believe this should resolve the linting issue.

Comment on lines +1 to +3
import { setupTestMatchers } from './src/abi/vitest.matcher';

setupTestMatchers();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move the contents of vitest.matcher to this file?

@@ -0,0 +1,5 @@
---
"fuels": minor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"fuels": minor

There are no changes to fuels.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this won't live in fuel-gauge which is our integration testing directory, vitest config should live outside here and be usable throughout the SDK.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @danielbate. These new matchers should be accessible for use in tests across any package.

The issue is that the created matchers rely on types imported from the math package. Ideally, these matchers should not depend on imported types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could move the setup to packages/utils/src/test-utils as @fuel-ts/utils is available at the root level as a devDependency.

It could also be helpful to end consumers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yaa that location makes sense since @fuel-ts/utils is available as a root devDependency and would make these matchers accessible across packages.

let me know if we want to go this way

Copy link
Contributor

@petertonysmith94 petertonysmith94 Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go for it @starc007 - makes sense to me :)

Will convert to draft until you're ready.

@petertonysmith94 petertonysmith94 marked this pull request as draft December 2, 2024 10:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore Issue is a chore good first issue Suitable for newcomers looking to contribute
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Integrate vitest matchers globally
5 participants